package org.proddevplm.business.project;

import java.util.List;
import java.util.Map;

import org.proddevplm.business.api.AController;
import org.proddevplm.business.project.constants.ProjectConstants;
import org.proddevplm.constants.ControllerConstants;
import org.proddevplm.dao.data.Project;
import org.proddevplm.dao.data.RelationMatrix;
import org.proddevplm.dao.data.Roles;
import org.proddevplm.dao.data.User;

public class ProjectController extends AController{

	@Override
	public Object execute(int command, Object data) {
		
	
		Map<String, Object> projectMap = (Map<String, Object>) data;
		Project currentProject;
		List<RelationMatrix> relMatrix;
		List<User> allUsers;
		List<Roles> allRoles;
		int projectId;
		
		switch(command){
		
		case ProjectConstants.ADD_PROJECT:
			
			Project project = new Project();
			
		    try{
		    	
		    	project.setName((String)projectMap.get("projectName"));
		    	project.setDescription((String)projectMap.get("projectDescription"));
		    	project.setStartDate((String)projectMap.get("startDate"));
		    	project.setEndDate((String)projectMap.get("endDate"));
		    	
		    	Map<Integer, User> usersMap = getDaoManager().getUserManager().getUsersById((String[])projectMap.get("usersArray"));
		    	Map<String, Roles> rolesMap = getDaoManager().getRoleManager().getRolesByName((String[])projectMap.get("rolesArray"));
		    	
		    	String[] usersArray = (String[]) projectMap.get("usersArray");
		    	String[] rolesArray = (String[]) projectMap.get("rolesArray");
		    	
		    	for (int index=0; index<usersArray.length; index++){
		    		User user = usersMap.get(Integer.valueOf(usersArray[index]));
		    		Roles role = rolesMap.get(rolesArray[index]);
		    		if (role==null){
		    			role = new Roles();
		    			role.setRole(rolesArray[index]);
		    		}
		    		getDaoManager().getProjectManager().addProject(project, user, role);
		    	}
		    	
		    	
			
            } catch (Exception e) {
            	e.printStackTrace();
            }
            putOnSession("projectId", project.getId());
            projectId = project.getId();
           
            //remove after correction
            currentProject = getDaoManager().getProjectManager().getProjectByProjectId(projectId);
            putOnSession("currentProject", currentProject);
            relMatrix = getDaoManager().getProjectManager().getRelMatrixByProjectId(projectId);
            putOnSession("relationMatrix", relMatrix);
            allUsers = getDaoManager().getUserManager().getUsers();
            putOnSession("allUsers", allUsers);
            allRoles = getDaoManager().getRoleManager().getAllRoles();
            putOnSession("allRoles", allRoles);
            
            putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab1");
			break;
			
		case ProjectConstants.OPEN_PROJECT:
			
			try{
				
				removeFromSession("currentProject");
				removeFromSession("currentProduct");
				projectId = Integer.valueOf((String)projectMap.get("projectId"));
				putOnSession("projectId", projectId);
				
				//remove after correction
				currentProject = getDaoManager().getProjectManager().getProjectByProjectId(projectId);
				putOnSession("currentProject", currentProject);
				allUsers = getDaoManager().getUserManager().getUsers();
	            putOnSession("allUsers", allUsers);
	            allRoles = getDaoManager().getRoleManager().getAllRoles();
	            putOnSession("allRoles", allRoles);
				
				relMatrix = getDaoManager().getProjectManager().getRelMatrixByProjectId(projectId);
				putOnSession("relationMatrix", relMatrix);
				
				
				
			}catch (Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
			putOnSession("currentTab", "tab1");
			break;
		
		case ProjectConstants.EDIT_PROJECT:
			relMatrix = (List)getSession().getAttribute("relationMatrix");
			RelationMatrix r = relMatrix.get(0);
			Project editProject = r.getProject();
			projectId = r.getProject().getId();
			currentProject = getDaoManager().getProjectManager().getProjectByProjectId(projectId);
			putOnSession("currentProject", currentProject);
			
			try{
				
				editProject.setName((String)projectMap.get("editProjectName"));
				editProject.setDescription((String)projectMap.get("editProjectDescription"));
				editProject.setStartDate((String)projectMap.get("editStartDate"));
				editProject.setEndDate((String)projectMap.get("editEndDate"));
				
				getDaoManager().getProjectManager().deleteRelMatrixByProjectId(projectId);
				
		    	String[] usersArray = (String[]) projectMap.get("editUsersArray");
		    	String[] rolesArray = (String[]) projectMap.get("editRolesArray");
		    	
		    	Map<Integer, User> usersMap = getDaoManager().getUserManager().getUsersById((String[])projectMap.get("editUsersArray"));
		    	Map<Integer, Roles> rolesMap = getDaoManager().getRoleManager().getRolesById((String[])projectMap.get("editRolesArray"));
		 	   	
		    	for (int index=0; index<usersArray.length; index++){
		    		User user = usersMap.get(Integer.valueOf(usersArray[index]));
		    		Roles role = rolesMap.get(Integer.valueOf(rolesArray[index]));
		    		if (role==null){
		    			role = new Roles();
		    			Roles aux = rolesMap.get(Integer.valueOf(rolesArray[index]));
		    			role.setRole(aux.getRole());
		    		}
		    		getDaoManager().getProjectManager().editProject(projectId, editProject, user, role);
		    	}
		    	
				
				
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectIndex.jsp");
			break;
		}
		return null;
	}

}